using System;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Core.Common
{
    public class FpsLimitQualityListener : 
        QualityListenerMono<FpsLimitQualityListener.FpsLimitQualitySetting>,
        IFpsLimitAdjuster
    {

        [Serializable]
        public class FpsLimitQualitySetting : QualitySetting
        {
            [Range(10,120)]
            public int fpsLimit = 30;

            [Range(10,120)]
            public int interactiveFpsLimit = 60;
        }


        public int fpsLimit = 30;
        /// <summary>
        /// 交互时的帧率
        /// </summary>
        public int interactiveFpsLimit = 60;

        private bool isInteracting = false;
        private int waitInteractingStateFrameCount = 0 ;

        public void ChangeFpsLevel(int level)
        {
            FpsLimitQualitySetting qualitySetting = null;

            if (level == 0)
            {
                qualitySetting = GetQualitySetting(QualityEnum.Low);
            }
            else if (level == 1)
            {
                qualitySetting = GetQualitySetting(QualityEnum.Medium);

            }
            else if (level == 2)
            {
                qualitySetting = GetQualitySetting(QualityEnum.VeryHigh);
            }
            else if (level == 3)
            {
                qualitySetting = GetQualitySetting(QualityEnum.Test);
            }

            if (qualitySetting!=null)
            {
                //....
               
                fpsLimit = qualitySetting.fpsLimit;
                interactiveFpsLimit = qualitySetting.interactiveFpsLimit;
                Application.targetFrameRate = getTargetFrameRate();
            }
        }

        private void Update()
        {
            if(waitInteractingStateFrameCount > 0)
            {
                waitInteractingStateFrameCount--;
                if(waitInteractingStateFrameCount == 0)
                {
                    isInteracting = false;
                }
            }
            int f = getTargetFrameRate();
            if (Application.targetFrameRate != f)
            {
                Application.targetFrameRate = f;
            }
        }

        private int getTargetFrameRate()
        {
            int targetRateLimit = DeviceAgent.Instance.HasOverrideTargetRate ? DeviceAgent.Instance.getTargetFps() : fpsLimit;  // 如果手动设置过fps，就用设置的；如果没有，则用默认的
            int f = isInteracting ? interactiveFpsLimit : targetRateLimit;  // 是否交互中
            return f;
        }

        /// <summary>
        /// 设置是否处于交互状态
        /// </summary>
        public void ChangeInteractingState(bool isInteracting)
        {
            if (!isInteracting) //如果在设置非交互状态时，等待1s再设置
            {
                waitInteractingStateFrameCount = 30; 
            }
            else  //设置交互状态时，立即设置
            {
                waitInteractingStateFrameCount = 0;
                this.isInteracting = isInteracting;
                Application.targetFrameRate = isInteracting ? interactiveFpsLimit : fpsLimit;
            }
            

           
        }
    }
}